package com.ky.facescan.utils;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.ky.facescan.R;


public class DialogUtils {
    /**
     * 初始化Dialog
     *
     * @param gravity Gravity.BOTTOM 弹出位置设置
     * @param color   弹出窗口背景颜色
     * @param width   宽度设置
     * @param height  高度设置
     * @param anim    出入动画
     */
    public static AlertDialog initDialog(Activity context, View view, int gravity, int color, float width, float height, int anim) {
        try {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setView(view);
            AlertDialog dialog = builder.create();
            dialog.show();
            //设置底部弹出 以及宽高设置
            Window window = dialog.getWindow();
            switch (gravity) {
                case Gravity.BOTTOM:
                    if (anim != 0) {
                        window.setWindowAnimations(anim);//动画
                    }
                    break;
            }
            //这一句消除白块
            window.setBackgroundDrawable(new BitmapDrawable());
            window.setGravity(gravity);//弹出方式
            //宽高设置 必须在show()方法之后 不然不生效
            WindowManager.LayoutParams p = dialog.getWindow().getAttributes(); //获取对话框当前的参数值
            p.width = (int) (UIUtils.getScreenWidth(context) * (width == 0 ? 1.0 : width)); //宽度设置为屏幕
            if (height > 0) {
                p.height = (int) (UIUtils.getScreenWidth(context) * height); //宽度设置为屏幕
            } else {
                p.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            }
            window.setDimAmount(0.3f);//灰度
            dialog.getWindow().setAttributes(p); //设置生效

            //设置背景铺满 以及白屏的问题
            dialog.getWindow().setBackgroundDrawable(null);
            dialog.getWindow().setBackgroundDrawableResource(color == 0 ? android.R.color.transparent : color);//背景颜色

            dialog.setCancelable(true);//点击外部取消 true:可以,false:不可以
            return dialog;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AlertDialog.Builder(context).create();
    }


    /**
     * 初始化Dialog
     *
     * @param gravity Gravity.BOTTOM 弹出位置设置
     * @param color   弹出窗口背景颜色
     * @param width   宽度设置
     */
    public static AlertDialog initDialog(Activity context, View view, int gravity, int color, float width) {
        return initDialog(context, view, gravity, color, width, 0, 0);
    }

    /**
     * @param context
     * @param contentText
     * @param negativeText
     * @param positiveText
     * @param listener
     * @return
     */
    public static void showCommonDialog(Context context, String contentText, String negativeText, String positiveText, IDialogItemListener listener) {
        View view = View.inflate(context, R.layout.dialog_common_two_btn, null);
        AlertDialog alertDialog = initDialog((Activity) context, view, Gravity.CENTER, 0, 0.7f);
        alertDialog.setCancelable(false);//点击外部取消 true:可以,false:不可以
        alertDialog.setCanceledOnTouchOutside(false);
        TextView content = view.findViewById(R.id.content);
        TextView positive = view.findViewById(R.id.positive);
        TextView negative = view.findViewById(R.id.negative);
        positive.setText(positiveText);
        negative.setText(negativeText);

        content.setText(contentText);

        positive.setOnClickListener(v -> {
            alertDialog.dismiss();
            listener.positive(alertDialog);
        });

        negative.setOnClickListener(v -> {
            alertDialog.dismiss();
            listener.negative(alertDialog);
        });
        alertDialog.show();
    }

    /**
     * @param context
     * @param contentText
     * @param positiveText
     * @return
     */
    public static void showSingleBtnDialog(Context context, String contentText, String positiveText, boolean cancelable, IDialogItemListener listener) {
        View view = View.inflate(context, R.layout.dialog_common_single_btn, null);
        AlertDialog alertDialog = initDialog((Activity) context, view, Gravity.CENTER, 0, 0.7f);
        alertDialog.setCancelable(cancelable);//点击外部取消 true:可以,false:不可以
        alertDialog.setCanceledOnTouchOutside(cancelable);
        TextView content = view.findViewById(R.id.content);
        TextView positive = view.findViewById(R.id.positive);
        positive.setText(positiveText);

        content.setText(contentText);

        positive.setOnClickListener(v -> {
            alertDialog.dismiss();
            listener.positive(alertDialog);
        });
        alertDialog.show();
    }


    public interface IDialogItemListener {
        void positive(Dialog dialog);

        void negative(Dialog dialog);
    }

    /**
     * @param context  上下文
     * @param callback 回调
     * @return
     */
    public static AlertDialog showLogoutDialog(Context context, View parentView, LogoutDialogCallback callback) {
        View view = View.inflate(context, R.layout.dialog_logout, null);
        AlertDialog alertDialog = initDialog((Activity) context, view, Gravity.CENTER, 0, 0.8f, 0, 0);
        TextView negative = view.findViewById(R.id.negative);
        TextView positive = view.findViewById(R.id.positive);
        EditText etAccount = view.findViewById(R.id.et_account);
        EditText etPwd = view.findViewById(R.id.et_pwd);

//        etAccount.requestFocus();
//        KeyBoardUtils.openKeyBoard(etAccount, context);
        negative.setOnClickListener(v -> {
            alertDialog.dismiss();

        });
        positive.setOnClickListener(v -> {
            String account = etAccount.getText().toString().trim();
            String pwd = etPwd.getText().toString().trim();

            if (TextUtils.isEmpty(account)) {
                ToastUtils.show("请输入账号");
                return;

            }
            if (TextUtils.isEmpty(pwd)) {
                ToastUtils.show("请输入密码");
                return;

            }
            callback.confirm(alertDialog, etAccount, etPwd, account, pwd);

        });
        alertDialog.setOnDismissListener(dialog -> {

//            KeyBoardUtils.timerHideKeyBoard(etAccount, 100);
            callback.dismiss();

        });
        alertDialog.setOnKeyListener((dialogInterface, keyCode, keyEvent) -> {
            if (keyCode == KeyEvent.KEYCODE_BACK && keyEvent.getRepeatCount() == 0)
                //返回键监听
                alertDialog.dismiss();
            return false;
        });
        return alertDialog;
    }


    public interface LogoutDialogCallback {
        void confirm(AlertDialog dialog, EditText etAccount, EditText etPassword, String account, String password);

        void dismiss();
    }
}
